package com.innovations.inn.controller;

import com.innovations.common.core.controller.BaseController;
import com.innovations.common.core.domain.AjaxResult;
import com.innovations.common.core.domain.entity.SysUser;
import com.innovations.inn.domain.Enterprise;
import com.innovations.inn.domain.ResearchInstitution;
import com.innovations.inn.service.IEnterpriseService;
import com.innovations.inn.service.IResearchInstitutionService;
import com.innovations.system.service.ISysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Api(tags = "用户信息")
@RestController
@RequestMapping("/inn/user")
public class UserController extends BaseController {
    @Autowired
    IEnterpriseService enterpriseService;
    @Autowired
    IResearchInstitutionService researchInstitutionService;
    @Autowired
    private ISysUserService userService;

    /**
     * 根据用户编号获取联系方式
     */
    @ApiOperation("根据用户编号获取联系方式")
    @PreAuthorize("@ss.hasPermi('inn:modify:list')")
    @GetMapping(value = {"/{userId}"})
    public AjaxResult getContactInfo(@PathVariable(value = "userId") Long userId) {
        AjaxResult ajax = AjaxResult.success();

        // 判断是否企业用户
        Enterprise enterprise = new Enterprise();
        enterprise.setUserId(userId);
        List<Enterprise> enterpriseList = enterpriseService.selectEnterpriseList(enterprise);
        if (enterpriseList != null && !enterpriseList.isEmpty()) {
            ajax.put("phone", enterpriseList.get(0).getEnterprisePhone());
            return ajax;
        }

        // 判断是否是科研机构
        ResearchInstitution researchInstitution = new ResearchInstitution();
        researchInstitution.setUserId(userId);
        List<ResearchInstitution> researchInstitutionList = researchInstitutionService.selectResearchInstitutionList(researchInstitution);
        if (researchInstitutionList != null && !researchInstitutionList.isEmpty()) {
            ajax.put("phone", researchInstitutionList.get(0).getPhone());
            return ajax;
        }

        // 其他用户
        SysUser user = new SysUser();
        user.setUserId(userId);
        List<SysUser> sysUserList = userService.selectUserList(user);
        if (sysUserList != null && !sysUserList.isEmpty()) {
            ajax.put("phone", sysUserList.get(0).getPhoneNumber());
            return ajax;
        }

        // 未找到
        ajax.put("phone", "");
        return ajax;
    }

}
